<?php
declare(strict_types=1);
/**
 * 标题文本组件
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/5/17
 * Time: 10:42:50
 */

namespace Service\LayoutService;

use Service\GoodsService\GoodsLinkService;
use Service\DecorateService\DecorateService;
use App\Model\BbcModel\Specialtopic;
use Service\ActivityService\ActivityService;

class titleText extends \Service\BaseService
{
    /**
     * @param $mnid
     * @param $cid
     * @param $mn_sid
     * @param $mid
     * @param $moduleData
     * @return array|\think\response\Json
     */
    public function getDataList($mnid,$cid,$mn_user,$mid,$moduleData,$extra){
        $mn_sid    = $mn_user['id'];
        $link = $moduleData['setStyle']['link'];

        if(!$link['link_id']){
            return $moduleData;
        }

        //跳转普通商品
        if($link['link_type'] == DecorateService::instance()->link_type['ordinaryGoods']){

            $goods_list = GoodsLinkService::instance()->getGoodsMnLinkList($mn_user,[$link['link_id']]);
            $goods_ids = array_column((array)$goods_list,'id');
            if(!isset($goods_ids[$link['link_id']])){ //商品不正常
                $link['link_id'] = '';
            }
        }

        //跳转专题馆
        if($link['link_type'] == DecorateService::instance()->link_type['subjectList']){
            $specialtopic_list = Specialtopic::instance()
                ->where(['cid'=>$cid,'df'=>0,'status'=>1])
                ->where('id','=',$link['link_id'])
                ->field('id,name')
                ->select()
                ->toArray();
            $specialtopic_ids = array_column($specialtopic_list,'id');

            if(!isset($specialtopic_ids[$link['link_id']])){
                $link['link_id'] = '';
            }
        }

        //跳转秒杀，拼团，满减
        if(
            $link['link_type'] == DecorateService::instance()->link_type['seckillList'] ||
            $link['link_type'] == DecorateService::instance()->link_type['groupBookingList'] ||
            $link['link_type'] == DecorateService::instance()->link_type['moneyOff']
        ){
            $returnActivity = ActivityService::instance()->getActivityIdsLink((int)$cid,(int)$mn_sid,$mn_user,[$link['link_id']],[]);
            $activity_ids_map = array_column($returnActivity,null,'activity_id');
            if(isset($activity_ids_map[$link['link_id']])){ //店铺存在此活动
                if($activity_ids_map[$link['link_id']]['object_status'] != 1){ //活动不正常
                    $link['link_id'] = '';
                }
                $link['target_type'] = $activity_ids_map[$link['link_id']]['target_type'] ?? 1;
            }else{
                $link['link_id'] = '';
                $link['target_type'] = 0;
            }
        }

        //跳转秒杀，拼团活动商品
        if(
            $link['link_type'] == DecorateService::instance()->link_type['seckillGoods'] ||
            $link['link_type'] == DecorateService::instance()->link_type['groupBookingGoods']
        ){
            $returnActivityGoods = ActivityService::instance()->getActivityIdsLink((int)$cid,(int)$mn_sid,$mn_user,[$link['link_id']],[$link['activity_id']]);
            $result = \Service\LayoutService\newImageAd::instance()->activity_goods($returnActivityGoods,$link['activity_id'],(int)$link['link_id']);
            if($result['object_status'] != 1){ //活动商品不正常
                $link['link_id'] = '';
            }
            $link['target_type'] = $result['target_type'] ?? 0;
        }
        $moduleData['setStyle']['link'] = $link;
        return $moduleData;
    }
}